/*
 * File Name:ms_tick_test.c
 * Version:v1.0.0
 * Change Logs:
 * Date             Author      	main change
 * 2025-03-13      	lanxin          the first version
 *
*/

/********************************************* READ ME BEGIN *************************************************************/

/* Brief:
 *
 * *1.对 ms_tick 的测试程序。
 *
 */

/* Notice:
 *
 * *1.ms_tick_increase is already called every millisecond.
 *
 */

/********************************************* READ ME END ***************************************************************/

/* source file */
#include "rtthread.h"
#include "ms_tick.h"
#include "stdio.h"

/* debug define */

/* private define */

/* function declaration */

/* variable declaration */

static void show_time ( time_t time )
{
	char time_string[64];
	ctime_r ( &time, time_string );
	printf ( "time string:%s", time_string );
}

static int do_show_ms_tick ( void )
{
	uint64_t tick = ms_tick_get();
	printf ( "ms tick:%llu\r\n", tick );
	return 0;
}

static int do_get_time ( void )
{
	uint64_t tick;
	struct timeval t_val;
	struct tm t_real;
	time_t _time;

	tick = ms_tick_get();
	time_t time_now = time ( 0 );
	ms_tick_get_timeval ( &t_val );
	ms_tick_get_time ( &_time );

	printf ( "ms tick:%llu\r\n", tick );

	show_time ( _time );

	show_time ( time_now );

	show_time ( t_val.tv_sec );
	printf ( " and %ld ms\r\n", t_val.tv_usec / 1000 );

	return 0;
}

static int do_set_time ( void )
{
	time_t t_now = 0;
	struct timeval tval_now;

	uint64_t tick = ( uint64_t ) 1741833890 * 1000;
	ms_tick_set ( tick );
	char time_string[64];
	t_now = time ( 0 );
	show_time ( t_now );

	tick += 5000;
	time_t t_set = tick / 1000;
	ms_tick_set_time ( &t_set );
	t_now = time ( 0 );
	show_time ( t_now );

	tick += 5000;
	struct timeval t_val = {.tv_sec = ( tick / 1000 ), .tv_usec = 5000};
	ms_tick_set_timeval ( &t_val );
	ms_tick_get_timeval ( &tval_now );
	show_time ( tval_now.tv_sec );
	printf ( "and %ld ms\r\n", tval_now.tv_usec / 1000 );

	return 0;
}

/* 导出到初始化列表 */

/* 导出到 MSH 命令列表 */
MSH_CMD_EXPORT ( do_show_ms_tick, show ms tick );
MSH_CMD_EXPORT ( do_get_time, get time );
MSH_CMD_EXPORT ( do_set_time, set time );
